<template>
  <div v-loading="loading">
    <tableComponents
      :pagination="{currentPage,pageSize,total:list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      :isShow="false"
      :isHaveExport="false"
      @toAdd="showEditDialog"
      addText="添加期数"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="期数" min-width="50">
            <template slot-scope="scope">{{ scope.row.stage }}</template>
          </el-table-column>

          <el-table-column label="大盘点数" min-width="50">
            <template slot-scope="scope">{{ scope.row.point |keepTwoNum}}</template>
          </el-table-column>

          <el-table-column label="买涨统计" min-width="50">
            <template slot-scope="scope">{{ scope.row.up_all }}</template>
          </el-table-column>

          <el-table-column label="买跌统计" min-width="50">
            <template slot-scope="scope">{{ scope.row.down_all }}</template>
          </el-table-column>

          <el-table-column label="服务费" min-width="50">
            <template slot-scope="scope">{{ scope.row.charge }}</template>
          </el-table-column>

          <el-table-column label="开始时间：" min-width="90">
            <template slot-scope="scope">{{scope.row.begin_time | getTimeFormat }}</template>
          </el-table-column>

          <el-table-column label="结束时间：" min-width="90">
            <template slot-scope="scope">{{scope.row.end_time | getTimeFormat }}</template>
          </el-table-column>

          <el-table-column label="操作" align="left" min-width="200">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                :disabled="scope.row.is_end=='1'"
                :type="scope.row.is_end=='0'? 'primary':'success'"
                @click="endGuessJieSuan(scope.row.id,scope.row.stage,scope.row.rf)"
              >{{scope.row.is_end=='0'? '去结算':'已结算'}}</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="endGuess(scope.row)"
              >查看</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                v-if="scope.row.is_end=='0'"
                type="primary"
                @click="showpoit(scope.row)"
              >录入大盘点数</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="deleteNew(scope.$index, scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="delSelection">删除</a>
      </template>
    </tableComponents>

    <GuessEdit @parent="reload" :show.sync="myshow" :editId="editId" :clickNum="clickNum"></GuessEdit>
    <el-dialog
      :close-on-click-modal="false"
      :title="!end?'录入大盘点数' :  '结果' "
      :visible.sync="dioForm"
      width="800px"
      class="tl size_13">
      <el-form label-position="right" label-width="120px" :model="form">
        <el-form-item label="大盘点数">
          <el-col :span="12">
            <el-input size="small" :disabled="end" clearable v-model="form.point"></el-input>
          </el-col>
        </el-form-item>
        <template v-if="end">
          <el-form-item label="买涨统计">
            <el-col :span="12">
              <el-input size="small" :disabled="true" clearable v-model="form.up_all"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="买跌统计">
            <el-col :span="12">
              <el-input size="small" :disabled="true" clearable v-model="form.down_all"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="平台利率千分比">
            <el-col :span="12">
              <el-input size="small" :disabled="true" clearable v-model="form.rate"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="平台利率千分比">
            <el-col :span="12">
              <el-input size="small" :disabled="true" clearable v-model="form.rate"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="服务费">
            <el-col :span="12">
              <el-input size="small" :disabled="true" clearable v-model="form.charge"></el-input>
            </el-col>
          </el-form-item>
          <!-- <el-form-item label="胜/负">
                        <el-col :span="12">
                            <el-input size="small" :disabled="true" clearable v-model="form.avg"></el-input>
                        </el-col>
          </el-form-item>-->
        </template>
        <el-form-item label="涨/跌">
          <el-col :span="12">
            <el-radio-group :disabled="end" v-model="form.rf">
              <el-radio :label="'0'">默认</el-radio>
              <el-radio :label="'1'">跌</el-radio>
              <el-radio :label="'2'">涨</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button :loading="$store.state.isLoading" size="small" @click="dioForm = false">取 消</el-button>
          <el-button
            :loading="$store.state.isLoading"
            size="small"
            v-if="!end"
            type="primary"
            @click="subpoitInfo"
          >确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import GuessEdit from "@/components/plugin/guess_edit.vue";

export default {
  data() {
    return {
      show: false,
      myshow: false,
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      editId: "",
      multipleSelection: "",
      arr: [],
      visible: false,
      clickNum: 1,
      updataIndex: "",
      searchAll: {},
      dioForm: false,
      form: {
        id: "",
        point: "",
        rf: ""
      },
      end: false,
      timepicker: ""
    };
  },
  components: {
    GuessEdit
  },
  methods: {
    showpoit(row) {
      this.form = row;
      this.dioForm = true;
      this.end = false;
    },
    endGuess(row) {
      this.form = row;
      this.dioForm = true;
      this.end = true;
    },
    endGuessJieSuan(id, stage, rf) {
      var rl = rf == 1 ? "跌" : "涨";
      this.$swal
        .fire({
          title: "是否结算?",
          text: "是否结算期号：" + stage + " 结果：" + rl,
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#3085d6",
          cancelButtonColor: "#d33",
          confirmButtonText: "结算",
          cancelButtonText: "不结算"
        })
        .then(result => {
          if (result.value) {
            var params = {};
            params.id = id;
            params.stage = stage;
            //  params.rf=rf;
            this.$api.getPlugin.endguess(params).then(res => {
              if (res.data.code == 200) {
                this.$fnc.alertSuccess({ text: "结算成功" });
                this.getNewsAll(this.searchAll);
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result);
              }
            });
          }
        });
    },
    subpoitInfo() {
      var params = {};
      if (!this.end) {
        console.log(this.form);
        params.id = this.form.id;
        params.point = this.form.point;
        params.rf = this.form.rf;
        this.$api.getPlugin.setpoitinfo(params).then(res => {
          console.log(res);
          if (res.data.code == 200) {
            this.$fnc.alertSuccess({ text: "录入成功" });
            this.dioForm = false;
          } else {
            this.$fnc.alertError(res.data.code_desc, res.data.result);
          }
        });
      }
    },
    showAddDialog() {
      this.show = true;
      console.log(this.show);
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getPlugin.getGuesslordList(params).then(res => {
        if (res.data.code == 200) {
          console.log(res);
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // console.log(`当前页:`+val)
                this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
    handleSelectionChange(val) {
      //多选框赋值
      this.multipleSelection = val;
    },
    delSelection() {
      //删除
      this.$swal({
        title: "您确定要删除所选信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var str = "";
          for (var i in this.multipleSelection) {
            console.log(this.multipleSelection[i]["id"]);
            str += this.multipleSelection[i].id + "@";
          }
          str = str.slice(0, -1);
          console.log(str);
          var data = { id_str: str };
          this.$api.getPlugin.delguess(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了所选信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    deleteNew(index, id) {
      console.log(id);
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getPlugin.delguess(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },
    showEditDialog(id) {
      this.myshow = true;
      this.editId = id;
      this.clickNum++;
      console.log(this.clickNum);
    },
    reload(id) {
      this.getNewsAll(this.searchAll);
    }
  },
  beforeCreate() {
    this.loading = true; //数据未加载之前调用loading
  },
  created() {
    this.getNewsAll(this.searchAll);
    this.loading = false;
  },
  computed: {},
  watch: {
    arr() {
      console.log("批量操作", this.arr);
    }
  }
};
</script>

<style lang="less" scoped>
thead > tr > th {
  height: 45px;
  line-height: 45px;
}
td,
th {
  border: 1px solid #e1e3e4;
  font-size: 12px;
}
th {
  background: #f8f8f8;
  font-weight: normal;
  color: #323437;
}
td {
  height: 75px;
  color: #000;
}
.caozuo {
  padding-left: 50px;
}
.pl {
  padding-left: 15px;
}
.chkBox {
  display: block;
  margin: 0 auto;
  width: 18px;
  height: 18px;
  background: #fff;
}
.aBtn_1 {
  padding: 10px;
}
</style>
